package cn.edu.gzhmt.goods.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import cn.edu.gzhmt.goods.domain.Goods;
import cn.edu.gzhmt.goods.service.IGoodsService;
import cn.edu.gzhmt.goods.service.impl.GoodsServiceImpl;
import cn.edu.gzhmt.utils.web.servlet.BaseServlet;

public class GoodsServlet extends BaseServlet {

	private IGoodsService goodsService = new GoodsServiceImpl();

	/**
	 * 查询所有商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Goods> goodsList = goodsService.findAllGoods();
		request.setAttribute("msg", "所有商品列表");

		request.setAttribute("goodsList", goodsList);

		return "f:/jsps/goods/goodsList.jsp";
	}

	/**
	 * 通过id查看商品信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String gid = request.getParameter("gid");

		Goods goods = goodsService.loadGoods(gid);

		request.setAttribute("goods", goods);

		return "f:/jsps/order/desc.jsp";
	}

	/**
	 * 按名称查询商品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findGoodsByName(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String gname = request.getParameter("goodsName");

		List<Goods> goodsList = null;
		// 如果名称为空，查询所有
		if (gname == null || gname.trim().length() <= 0) {
			goodsList = goodsService.findAllGoods();
		} else {
			goodsList = goodsService.findGoodsByName(gname);
		}
		request.setAttribute("goodsList", goodsList);

		return "f:/jsps/goods/goodsList.jsp";
	}

	/**
	 * 主页进行按分类加载商品
	 * 
	 * 通过名称分类id查询商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findGoodsByMain(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取参数：分类id
		String cid = request.getParameter("cid");
		if (cid == null || cid.trim().length() <= 0) {
			return null;
		}

		// 通过根据分类查询商品
		List<Goods> goodsList = goodsService.findGoodsByMain(cid);

		JSONArray json = JSONArray.fromObject(goodsList);
		//System.out.println("json = " + json.toString());

		response.getWriter().print(json.toString());

		return null;
	}
	
	/**
	 * 通过分类查询商品
	 * 查询分类下的商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findGoodsByCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 获取参数：分类id
		String cid = request.getParameter("cid");
		if (cid == null || cid.trim().length() <= 0) {
			return null;
		}

		// 通过根据分类查询商品
		List<Goods> goodsList = goodsService.findGoodsByCategory(cid);
		request.setAttribute("goodsList", goodsList);

		return "f:/jsps/goods/goodsList.jsp";
	}
	
	/**
	 * 加载热卖商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadHotGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// 进行加载热卖商品
		List<Goods> goodsList = goodsService.loadHotGoods();
		
		JSONArray json = JSONArray.fromObject(goodsList);
		response.getWriter().print(json.toString());

		return null;
	}
	
	/**
	 * 加载推荐商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String recommendGoods(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// 进行加载推荐商品
		List<Goods> goodsList = goodsService.recommendGoods();
		
		JSONArray json = JSONArray.fromObject(goodsList);
		response.getWriter().print(json.toString());
		
		return null;
	}
	
	
	

}
